package com.yy.wga.mp.module.goods.task;

import com.yy.wga.mp.module.goods.entity.pojo.GoodsSpuDO;
import com.yy.wga.mp.module.goods.response.GoodsInfoResp;
import com.yy.wga.mp.common.interceptor.RequestUserHolder;
import com.yy.wga.mp.common.utils.common.StringUtils;
import com.yy.wga.mp.common.utils.mapping.BeansUtils;
import com.yy.wga.mp.module.goods.api.request.product.GetProductPromotionInfoReq;
import com.yy.wga.mp.module.goods.api.request.product.GetPromoteProductDetailReq;
import com.yy.wga.mp.module.goods.api.response.product.PromoteProductDetailDTO;
import com.yy.wga.mp.module.goods.api.response.product.PromoterSingleProductPromotionInfoDTO;
import com.yy.wga.mp.module.goods.common.utils.CommissionUtil;
import com.yy.wga.mp.module.promoter.service.IProductService;
import com.yy.wga.mp.module.user.enums.EUserGrade;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.Callable;

@AllArgsConstructor
@Data
@Builder
public class GetPromoteGoodsTask implements Callable<GoodsInfoResp> {

    private Integer planType;
    private String shopAppid;
    private String productId;
    private IProductService productService;
    private EUserGrade userGrade;
    private String appKey;
    private String openId;


    @Override
    public GoodsInfoResp call() throws Exception {

        BaseRequestDTO baseRequestDTO = new BaseRequestDTO();
        baseRequestDTO.setAppKey(appKey);
        RequestUserHolder.setBaseRequest(baseRequestDTO);

        GetPromoteProductDetailReq getPromoteProductDetailReq = new GetPromoteProductDetailReq();
        getPromoteProductDetailReq.setProductId(productId);
        getPromoteProductDetailReq.setShopAppid(shopAppid);
        getPromoteProductDetailReq.setPlanType(2);

        PromoteProductDetailDTO promoteProductDetail = productService.getPromoteProductDetail(getPromoteProductDetailReq);
        PromoteProductDetailDTO.Product productResp = promoteProductDetail.getProduct();
        GoodsSpuDO goodsSpu = toGoodsSpu(productResp);


        Integer commissionValue = CommissionUtil.calcCommissionValue(goodsSpu.getSalePrice(), productResp.getCommissionInfo().getServiceRatio(), userGrade);

        GoodsInfoResp goodsInfoResp = BeansUtils.map(goodsSpu, GoodsInfoResp.class);
        // 商品详情的不要
        goodsInfoResp.setProductPromotionLink(null);
        goodsInfoResp.setHeadImg(goodsSpu.getHeadImg().get(0));
        goodsInfoResp.setCommissionValue(commissionValue);

        // 使用转链的接口
        GetProductPromotionInfoReq getProductPromotionInfoReq = new GetProductPromotionInfoReq();
        getProductPromotionInfoReq.setProductId(Long.valueOf(productId));
        getProductPromotionInfoReq.setShopAppid(shopAppid);
        getProductPromotionInfoReq.setSharerOpenid(openId);
        PromoterSingleProductPromotionInfoDTO promoterSingleProductPromotionInfo = productService.getPromoterSingleProductPromotionInfo(getProductPromotionInfoReq);
        String productPromotionLink = promoterSingleProductPromotionInfo.getProductPromotionLink();
        goodsInfoResp.setProductPromotionLink(productPromotionLink);
        return goodsInfoResp;
    }

    /**
     * 同步商品信息
     *
     * @param productResp productResp
     */
    public static GoodsSpuDO toGoodsSpu(PromoteProductDetailDTO.Product productResp) {
        GoodsSpuDO goodsSpu = new GoodsSpuDO();
        PromoteProductDetailDTO.ProductInfo productInfo = productResp.getProductInfo();

        int stockNum = 0;
        List<PromoteProductDetailDTO.Sku> skuResps = productInfo.getSkus();
        Integer salePrice = skuResps.get(0).getSalePrice();
        List<GoodsSpuDO.Sku> skuInfos = new ArrayList<>();
        for (PromoteProductDetailDTO.Sku skuResp : skuResps) {
            stockNum += skuResp.getStockNum();
            List<GoodsSpuDO.Attr> goodsAttrs = new ArrayList<>();
            for (PromoteProductDetailDTO.Attr attr : skuResp.getSkuAttrs()) {
                String attrValue = attr.getAttrValue();
                if (StringUtils.isNotBlank(attrValue)) {
                    GoodsSpuDO.Attr goodsAttr = new GoodsSpuDO.Attr();
                    goodsAttr.setAttrKey(attr.getAttrKey());
                    goodsAttr.setAttrValue(Arrays.asList(attrValue.split(";")));
                    goodsAttrs.add(goodsAttr);
                }
            }
            GoodsSpuDO.Sku sku = BeansUtils.map(skuResp, GoodsSpuDO.Sku.class);
            sku.setSkuAttrs(goodsAttrs);
            skuInfos.add(sku);
        }
        PromoteProductDetailDTO.DescInfo descInfo = productInfo.getDescInfo();
        if (Objects.nonNull(descInfo)) {
            goodsSpu.setDetailImg(descInfo.getImgs());
        }
        goodsSpu.setSkus(skuInfos);
        goodsSpu.setShopAppid(productResp.getShopAppid());
        goodsSpu.setSpuId(productResp.getProductId());
        goodsSpu.setTitle(productInfo.getTitle());
        goodsSpu.setSubTitle(productInfo.getSubTitle());
        goodsSpu.setHeadImg(productInfo.getHeadImgs());
        goodsSpu.setStockNum(stockNum);
        goodsSpu.setSalePrice(salePrice);
        goodsSpu.setMarketPrice(salePrice);
        goodsSpu.setProductPromotionLink(productInfo.getProductPromotionLink());
        List<PromoteProductDetailDTO.Cat> catsV2 = productInfo.getCatsV2();
        if (!CollectionUtils.isEmpty(catsV2)) {
            int index = 1;
            for (PromoteProductDetailDTO.Cat cat : catsV2) {
                String catId = cat.getCatId();
                switch (index) {
                    case 1:
                        goodsSpu.setFirstCateId(catId);
                        break;
                    case 2:
                        goodsSpu.setSecondCateId(catId);
                        break;
                    case 3:
                        goodsSpu.setThirdCateId(catId);
                        break;
                }
                index++;
            }
        }
        // 设置为上架状态
        goodsSpu.setStatus(5);
        return goodsSpu;
    }
}